package com.lemon.smartsafetynew.ui.adapter.record;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecycleChildItemClickListener;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.record.EquipVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by lzh on 2018/3/18.
 */

public class EquipInfoAdapter extends LemonRcvAdapter<EquipVo> {
    private RecycleChildItemClickListener<EquipVo> mListener;

    public EquipInfoAdapter(@Nullable List<EquipVo> data, RecycleChildItemClickListener<EquipVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new EquipItem();
    }

    public class EquipItem implements AdapterItem<EquipVo> {
        @Bind(R.id.tv_space_name)
        TextView tvSpaceName;
        @Bind(R.id.tv_equip_type)
        TextView tvEquipType;
        @Bind(R.id.tv_out_company_type)
        TextView tvOutCompanyType;
        @Bind(R.id.tv_out_company_code)
        TextView tvOutCompanyCode;
        @Bind(R.id.tv_register_number)
        TextView tvRegisterNumber;
        @Bind(R.id.tv_contact_people)
        TextView tvContactPeople;
        @Bind(R.id.tv_contact_number)
        TextView tvContactNumber;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.item_layout)
        LinearLayout itemLayout;
        @Bind(R.id.frameLayout)
        FrameLayout frameLayout;
        private EquipVo data;
        private int pos;

        @Override
        public int getLayoutResId() {
            return R.layout.layout_equip_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data, pos);
                }
            });

            tvContactNumber.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onChildItemClick(data, pos);
                }
            });
        }

        @Override
        public void handleData(EquipVo equipVo, int position) {
            data = equipVo;
            pos = position;
            tvSpaceName.setText(CommonUtil.avoidNullString(equipVo.getData_true_name()));
            tvEquipType.setText(CommonUtil.avoidNullString(equipVo.getData_name_big()));
            tvOutCompanyType.setText(CommonUtil.avoidNullString(equipVo.getModel()));
            tvOutCompanyCode.setText(CommonUtil.avoidNullString(equipVo.getFactory_number()));
            tvRegisterNumber.setText(CommonUtil.avoidNullString(equipVo.getRegister_code()));
            tvContactPeople.setText(CommonUtil.avoidNullString(equipVo.getLinkman()));
            tvContactNumber.setText(CommonUtil.avoidNullString(equipVo.getPhone()));
            if (equipVo.getDayStatus() != null) {
                String status = equipVo.getDayStatus(); //0 正常  1快过期 2已过期
                if (status.equals("0")) {
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp1);
                    tvDate.setTextColor(Color.parseColor("#458bd0"));
                } else if (status.equals("1")) {
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp2);
                    tvDate.setTextColor(Color.parseColor("#FF5B00"));
                } else if (status.equals("2")) {
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp3);
                    tvDate.setTextColor(Color.parseColor("#FF0202"));
                }
            }
            if (equipVo.getRecord_date() != null) {
                tvDate.setText("有效期至\n" + CommonUtil.secondToTimeStr(equipVo.getRecheck_date(), DateTypeEnum.DAY));
            }
        }
    }
}
